#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
 
#include "globlvar_debug.h"
#include "wdgfun.h"
#include "systickapp.h"
#include "pub_base_prog.h"

volatile struct STTICK sttickint;
struct STTICK sttick;
#ifndef	_freertosuse_
volatile long lsystickstatu;
#endif
#if  _systick_2_rtc_ ==1
_tyTIMECNT lRtcTimebak;
#endif

__weak void
SYSTICK_AppCallback (void)
{

}

void
mainbeat_intfun (void)
{
  static unsigned short iHalfsectm;
  static unsigned short iSectm;
  static unsigned short i166ms;
  static unsigned short i100ms;
  const unsigned short iSecSetTm = 1000 / _systick_step_time_ms_;
  const unsigned short iHalfSecTm = iSecSetTm / 2;
  const unsigned short i166MS = 166 / _systick_step_time_ms_;
  const unsigned short i100MS = 100 / _systick_step_time_ms_;

  _bInt_SysTick_io = 1;
#if _systick_bz_Keep_ ==1
  _bInt_SecEvent_io = ((++iSectm >= iSecSetTm) ||(  _bInt_SecEvent_io)) ? 1 : 0;
#else
	_bInt_SecEvent_io = ((++iSectm >= iSecSetTm) ||( 0)) ? 1 : 0;
#endif
	
  if (iSectm >= iSecSetTm)
    {
      iSectm = 0;
      _bInt_SecBit_io = ~_bInt_SecBit_io;
			_cSecIntno_io++;
			#if  _systick_2_rtc_ ==1
			_lSecIntno_io++;
			RTC_Int_CallBack ();
			#endif      
    }
#if _systick_bz_Keep_ ==1		
  _bInt_HalfSecEvent_io = ((++iHalfsectm >= iHalfSecTm) ||(_bInt_HalfSecEvent_io))? 1 : 0;
#else		
 _bInt_HalfSecEvent_io = ((++iHalfsectm >= iHalfSecTm) ||(0))? 1 : 0;		
#endif
  if (iHalfsectm >= iHalfSecTm)
    {
      iHalfsectm = 0;
			_cHalfSecIntno_io++;
      _bInt_HalfSec_io = ~_bInt_HalfSec_io;
      //      _cHalfSecIntno_io++;
    }
  if (++i166ms >= i166MS)
    {
      i166ms = 0;
      _bInt_b300ms_io = ~_bInt_b300ms_io;
    }
#if _systick_bz_Keep_ ==1		
		_bInt_b100msEvent_io  = ((++i100ms >= i100MS) || (	_bInt_b100msEvent_io))?1:0;
#else
		_bInt_b100msEvent_io  = ((++i100ms >= i100MS) || (	0))?1:0;
#endif
  if (_bInt_b100msEvent_io)
    {
      i100ms = 0;
			_bInt_b100msEvent_io =1;
			_c100MsIntno_io++;
      _bInt_b100ms_io = ~_bInt_b100ms_io;
    }
}
 
/*
	demo  
const   unsigned short *iTimerLIst[] =
{
	(unsigned short *)&stio.stcanstatu.iCanWaitTm,
	(unsigned short *)&stio.stcanstatu.iCanComSpaceTm,
	(unsigned short *)&stio.stcanstatu.iCanComTm,
	(unsigned short *)&stio.iR485ComTm,
	_iReg_Getp(_iAlmTestTm_),
  _iReg_Getp (_FlashSave_Tm_),
	_iReg_Getp (_iOutRefreshTm_),
}	;	
*/

void
SysTick_Handler_io (void)
{
  _mainBeatInono_Inc ();
  mainbeat_intfun ();
  Wdg_Fun_Int ();
}

void
HAL_SYSTICK_Callback (void)
{
  debug_break (_debug_errno_SysTick_Handler_);
  SysTick_Handler_io ();
  SYSTICK_AppCallback ();
  return; 
}


